package cn.yeyiu.util;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.view.Surface;


/**
 * 自定义工具类
 * <p>
 * 实时的检测用户屏幕的位置改变，做到 重力感应 与 全屏/半屏按钮 逻辑的互容
 */
public class OrientationUtil {
    /**
     * -1  未设置
     * 0 - 竖屏
     * 1 - 左横屏(正向)
     * 2 - 反向竖屏
     * 3 - 右横屏（反向）
     * 4 - 横屏传感器
     * 5 - 竖屏传感器
     */
    public static final int UNSET = -1;
    public static final int PORTRAIT = 0;
    public static final int LANDSCAPE = 1;
    public static final int REVERSE_PORTRAIT = 2;
    public static final int REVERSE_LANDSCAPE = 3;
    public static final int LANDSCAPE_SENSOR = 4;
    public static final int PORTRAIT_SENSOR = 5;

    /**
     * 获取当前屏幕旋转角度
     *
     * @return 0 - 竖屏
     * 1 - 左横屏(正向)
     * 2 - 反向竖屏
     * 3 - 右横屏（反向）
     */
    public int getDisplayRotation(Activity mActivity) {
        return mActivity.getWindowManager().getDefaultDisplay().getRotation();
    }

    // 设置屏幕方向
    public static void setDisplayRotation(Activity mActivity, int rotation) {
        switch (rotation) {
            case UNSET:
                mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
                break;
            case PORTRAIT:
                mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                break;
            case LANDSCAPE:
                mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                break;
            case REVERSE_PORTRAIT:
                mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
                break;
            case REVERSE_LANDSCAPE:
                mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
                break;
            case LANDSCAPE_SENSOR:
                mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
                break;
            case PORTRAIT_SENSOR:
                mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
                break;
        }
    }

    // 初始化屏幕
    public static void cancelDisplayRotation(Activity mActivity) {
        mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
    }

    // 横竖 交换
    public static void swipeDisplayOrientation(Activity mActivity) {
        int orientation = mActivity.getRequestedOrientation();

        if (orientation == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
            setDisplayRotation(mActivity, PORTRAIT);
        } else if (orientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
            setDisplayRotation(mActivity, LANDSCAPE);

        } else if (orientation == ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE) {
            setDisplayRotation(mActivity, REVERSE_PORTRAIT);
        } else if (orientation == ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT) {
            setDisplayRotation(mActivity, REVERSE_LANDSCAPE);

        } else if (orientation == ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE) {
            setDisplayRotation(mActivity, PORTRAIT_SENSOR);
        } else if (orientation == ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT) {
            setDisplayRotation(mActivity, LANDSCAPE_SENSOR);
        }
    }

    // 改变横竖 状态
    public static void sensorDisplayOrientation(Activity mActivity, boolean isHorizontal) {
        if (isHorizontal) {
            setDisplayRotation(mActivity, LANDSCAPE_SENSOR);
        } else {
            setDisplayRotation(mActivity, PORTRAIT_SENSOR);
        }
    }
}
